package org.chz.utils;

import com.huaban.analysis.jieba.JiebaSegmenter;
import com.huaban.analysis.jieba.SegToken;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Component
@Scope("singleton")
public class SplitUtil {
    private final JiebaSegmenter segmenter = new JiebaSegmenter();
    private Set<String> stopWords = new HashSet<>();

    @PostConstruct
    private void init() {
        // 读取停止词文件
        try (BufferedReader br = new BufferedReader(new InputStreamReader(
                getClass().getClassLoader().getResourceAsStream("stop_words.utf8")))) {
            String line;
            while ((line = br.readLine()) != null) {
                stopWords.add(line.trim());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Jieba分词
     * @param text
     * @return
     */
    public List<String> segment(String text) {
        List<SegToken> tokens = segmenter.process(text, JiebaSegmenter.SegMode.INDEX);
        return tokens.stream()
                .map(token -> token.word)
                .filter(word -> !word.trim().isEmpty()) // 过滤掉空格
                .filter(word -> !stopWords.contains(word)) // 过滤掉停止词
                .collect(Collectors.toList());
    }


    // 方法用于将字符串中的中文单字和英文字符按顺序拆分并存入ArrayList中
    public ArrayList<String> splitString(String input) {
        ArrayList<String> result = new ArrayList<>();

        // 匹配中文字符（一个汉字的Unicode范围是 \u4e00-\u9fa5）
        Pattern chinesePattern = Pattern.compile("[\\u4e00-\\u9fa5]");
        Matcher chineseMatcher = chinesePattern.matcher(input);

        // 匹配英文字符（包括大写和小写）
        Pattern englishPattern = Pattern.compile("[a-zA-Z]");
        Matcher englishMatcher = englishPattern.matcher(input);

        // 先处理中文字符
        while (chineseMatcher.find()) {
            result.add(chineseMatcher.group());
        }

        // 再处理英文字符
        while (englishMatcher.find()) {
            result.add(englishMatcher.group());
        }

        return result;
    }
}
